<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Create and Define some functions for global use
 *
 * @author Samuel Pulta
 */
class Functions extends library{
    
    /* User to encrypt any variable and decrypt it */
    /* ====================================================== */
    
        public function encrypt_decrypt($action, $string) {
            $output = false;
    
            //$key = 'GLOBALSTRATEGICSOLUTIONANDSERVICESINC_FORMALISTICS';
            $key = 'MyNaturalSuperStrongRandomKey';
        
            // initialization vector 
            $iv = md5(md5(md5($key)));
    
            if ($action == 'encrypt') {
                $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(md5(md5($key))), $string, MCRYPT_MODE_CBC, md5(md5(md5($iv))));
                $output = base64_encode($output);
            } else if ($action == 'decrypt') {
                $output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5(md5(md5($key))), base64_decode($string), MCRYPT_MODE_CBC, md5(md5(md5($iv))));
                $output = rtrim($output);
            }
            return $output;
        }
        
    /* Get Url of the page */
    /* ====================================================== */
    
        public function curPageURL($params) {
            $pageURL = 'http';
            $pageURL .= "://";
            if ($params == "?") {
                $par = $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
                $par = explode("?", $par);
                $pageURL .= $par[0];
            } elseif ($params == "") {
                if ($_SERVER["HTTPS"] == "on") {
                    $pageURL .= "s";
                }
                if ($_SERVER["SERVER_PORT"] != "80") {
                    $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
                } else {
                    $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
                }
            } elseif ($params == "module") {
                if ($_SERVER["HTTPS"] == "on") {
                    $pageURL .= "s";
                }
                if ($_SERVER["SERVER_PORT"] == "80") {
                    $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
                } else {
                    $pageURL = substr($_SERVER["REQUEST_URI"], 1);
                    // $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
                }
            } elseif ($params == "module_parameter") {
                $pageURL = substr($_SERVER["REQUEST_URI"], 1);
            } elseif ($params == "serverName") {
                $pageURL .= $_SERVER["SERVER_NAME"];
            } elseif ($params == "port") {
                $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
            } elseif ($params == "baseuri") {
                if ($_SERVER["HTTPS"] == "on") {
                    $pageURL .= "s";
                }
                if ($_SERVER["SERVER_PORT"] != "80") {
                    $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
                } else {
                    $pageURL .= $_SERVER["SERVER_NAME"];
                }
            }
    
            return $pageURL;
        }
        
    /* Encode and Decode URL */
    /* ====================================================== */
    
        public function encode_decode_url($url,$type){
            if($type=="encode"){
                return urlencode($url);
            }else{
                return urldecode($url);
            }
        }
        
    /* URL REDIRECT  */
    /* ====================================================== */
        
        public function url_redirect(){
            $current_url = $this->encode_decode_url($this->curPageURL("module_parameter"),"encode");

            $url = $_SERVER['REQUEST_URI'];
            if($url!=""){
                header('location: /login?redirect_url=' . $current_url);
            }else{
                header('location: /login');
            }
        }
    
    /* AVATAR VIEW  */
    /* ====================================================== */
        /*
         * @userType = {companyAdmin / companyUser}
         * @$idType = {user ID  / company ID}
         * @width = width of the image
         * @height = height of the image
         * @size = {small,medium,large}
         * @class = image design/class/method
         * @style = add another inline style for your avatar
         */

    public function avatarPic($userType, $idType, $width, $height, $size, $class, $style) {
        //$serverName = $this->curPageURL("port");
        $serverName = EXTERNAL_IMAGES;
        $is_active = "1";

        if ($userType == "companyAdmin") {
            $table = "tbcompany";
            $getExtension = $this->query("SELECT * FROM {$table} WHERE id={$this->escape($idType)} AND is_active={$this->escape($is_active)}", "row");
            $title = $getExtension['name'];
        } else {
            $table = "tbuser";
            $getExtension = $this->query("SELECT * FROM {$table} WHERE id={$this->escape($idType)} AND is_active={$this->escape($is_active)}", "row");
            $title = $getExtension['first_name'] . ' ' . $getExtension['last_name'];
        }
        $ext = $getExtension['extension'];
        $id_encrypt = md5(md5($idType));
        if ($ext != "") {
            $img = '<img data-placement="bottom" data-original-title="' . $title . '" src="' . $serverName . 'formalistics/' . $table . '/' . $id_encrypt . '/' . $size . '_' . $id_encrypt . '.' . $ext . '" width="' . $width . '" height="' . $height . '" class="' . $class . ' userAvatar" ' . $style . '>';
        } else {
            $img = '<img data-placement="bottom" data-original-title="' . $title . '" src="' . $serverName . 'avatar/large.png" width="' . $width . '" height="' . $height . '" class="' . $class . ' userAvatar" ' . $style . '>';
        }
        return $img;
    }
    
    /*
     * Arrange json formatter 
     * for better viewing of json_encode
     */

    public function view_json_formatter($string) {
        $pattern = array(',"', '{', '}');
        $replacement = array(",\n\t\"", "\n{\n\t", "\n}");
        return str_replace($pattern, $replacement, $string);
    }
}


?>